﻿using System;
using Newtonsoft.Json;
using System.Web.Http;
using Ymatou.API.Common;
using Ymatou.CommonService;
using Ymatou.Evaluate.Buyer.Model.ApiModel.Request.Operator;
using Ymatou.Evaluate.Buyer.Model.ApiModel.Response.Operator;
using Ymatou.PerfMonitorClient;

namespace BuyerEvaluateOperator.Api.Controllers
{
    public class EvaluateOperatorController : BaseController
    {
        /// <summary>
        /// 初始化评价状态
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("api/InitEvaluate")]
        [HttpPost]
        public string InitEvaluate([FromBody]InitEvaluateRequest request)
        {
            using (MethodMonitor.New("api/InitEvaluate"))
            {
                try
                {
                    ApplicationLog.Info(string.Format("api/InitEvaluate请求Request：{0}", JsonConvert.SerializeObject(request)));
                    if (request == null) throw new BussniessException("-1", "请求参数不能传入null");
                    request.Validate();
                    string msg = "";
                    var result = EvaluateOperatorService.AddEvaluateStatus(request.OrderId, request.SellerId, request.BuyerId, out msg);
                    ApplicationLog.Info(string.Format("api/InitEvaluate请求Response：{0}", JsonConvert.SerializeObject(result)));
                    if (result || !string.IsNullOrEmpty(msg))
                    {
                        if (!string.IsNullOrEmpty(msg))
                            ApplicationLog.Info(string.Format("api/InitEvaluate业务异常：Msg={0},Request={1}", msg, JsonConvert.SerializeObject(request)));
                        return "ok";
                    }
                    else
                    {
                        return "fail";
                    }
                }
                catch (BussniessException ex)
                {
                    ApplicationLog.Info(string.Format("api/InitEvaluate业务异常：Msg={0},Request={1}", ex.ErrorMsg, JsonConvert.SerializeObject(request)));
                    return "ok";
                }
                catch (Exception ex)
                {
                    ApplicationLog.Error(string.Format("api/InitEvaluate异常 请求Request：{0}", JsonConvert.SerializeObject(request)), ex);
                    return "fail";
                }
            }
        }

        /// <summary>
        /// 删除评价
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("api/RemoveEvaluate")]
        [HttpPost]
        public BaseResponseModel<RemoveEvaluateResponse> RemoveEvaluate([FromBody]RemoveEvaluateRequest request)
        {
            using (MethodMonitor.New("api/RemoveEvaluate"))
            {
                try
                {
                    ApplicationLog.Info(string.Format("api/RemoveEvaluate请求Request：{0}", JsonConvert.SerializeObject(request)));
                    if (request == null) throw new BussniessException("-1", "请求参数不能传入null");
                    request.Validate();

                    string msg = "";
                    var response = EvaluateOperatorService.RemoveEvaluate(request, out msg);
                    ApplicationLog.Info(string.Format("api/RemoveEvaluate请求Response：{0}", JsonConvert.SerializeObject(response)));
                    if (response.IsSuccess)
                    {
                        return new SuccessResponseModel<RemoveEvaluateResponse>(response);
                    }
                    else
                    {
                        return new BussniessErrorResponseModel<RemoveEvaluateResponse>(-1, msg);
                    }
                }
                catch (BussniessException ex)
                {
                    ApplicationLog.Info(string.Format("api/RemoveEvaluate业务异常：Msg={0},Request={1}", ex.ErrorMsg, JsonConvert.SerializeObject(request)));
                    return new BussniessErrorResponseModel<RemoveEvaluateResponse>(int.Parse(ex.ErrorCode), ex.ErrorMsg);
                }
                catch (Exception ex)
                {
                    ApplicationLog.Error(string.Format("api/RemoveEvaluate异常 请求Request：{0}", JsonConvert.SerializeObject(request)), ex);
                    return new SystemErrorResponseModel<RemoveEvaluateResponse>(ex.Message);
                }
            }
        }

        /// <summary>
        /// 提交评价
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("api/SubmitEvaluate")]
        [HttpPost]
        public BaseResponseModel<SubmitEvaluateResponse> SubmitEvaluate([FromBody]SubmitEvaluateRequest request)
        {
            using (MethodMonitor.New("api/SubmitEvaluate"))
            {
                try
                {
                    ApplicationLog.Info(string.Format("api/SubmitEvaluate请求Request：{0}", JsonConvert.SerializeObject(request)));
                    if (request == null) throw new BussniessException("-1", "请求参数不能传入null");
                    request.Validate();

                    string msg = "";
                    var response = EvaluateOperatorService.SubmitEvaluate(request, out msg);
                    ApplicationLog.Info(string.Format("api/SubmitEvaluate请求Response：{0}", JsonConvert.SerializeObject(response)));
                    if (response.IsSuccess)
                    {
                        return new SuccessResponseModel<SubmitEvaluateResponse>(response);
                    }
                    else
                    {
                        return new BussniessErrorResponseModel<SubmitEvaluateResponse>(-1, msg);
                    }
                }
                catch (BussniessException ex)
                {
                    ApplicationLog.Info(string.Format("api/SubmitEvaluate业务异常：Msg={0},Request={1}", ex.ErrorMsg, JsonConvert.SerializeObject(request)));
                    return new BussniessErrorResponseModel<SubmitEvaluateResponse>(int.Parse(ex.ErrorCode), ex.ErrorMsg);
                }
                catch (Exception ex)
                {
                    ApplicationLog.Error(string.Format("api/SubmitEvaluate异常 请求Request：{0}", JsonConvert.SerializeObject(request)), ex);
                    return new SystemErrorResponseModel<SubmitEvaluateResponse>(ex.Message);
                }
            }
        }

        /// <summary>
        /// 自动评价
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("api/AutoEvaluate")]
        [HttpPost]
        public string AutoEvaluate([FromBody]AutoEvaluateRequest request)
        {
            if (request == null) request = new AutoEvaluateRequest();
            if (request.OrderIds != null && request.OrderIds.Count > 100) return "订单数不能超过100";
            EvaluateOperatorService.AutoEvaluate(request);
            return "ok";
        }

        /// <summary>
        /// 重置评价数据
        /// </summary>
        /// <param name="ProductId"></param>
        /// <returns></returns>
        [Route("api/ResetEvaluate")]
        [HttpPost]
        public string ResetEvaluate([FromBody]ResetEvaluateRequest request)
        {
            EvaluateOperatorService.ResetEvaluate(request);
            return "ok";
        }

        /// <summary>
        /// 评价洗数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("api/AutoSubmitEvaluate")]
        [HttpPost]
        [Obsolete("洗数据用")]
        public string AutoSubmitEvaluate([FromBody]AutoSubmitEvaluateRequest request)
        {
            if (request == null) request = new AutoSubmitEvaluateRequest();
            if (request.OrderIds != null && request.OrderIds.Count > 100) return "订单数不能超过100";
            EvaluateOperatorService.AutoSubmitEvaluate(request);
            return "ok";
        }

        /// <summary>
        /// 评价洗数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("api/AutoInitEvaluate")]
        [HttpPost]
        [Obsolete("洗数据用")]
        public string AutoInitEvaluate([FromBody]AutoSubmitEvaluateRequest request)
        {
            if (request == null) request = new AutoSubmitEvaluateRequest();
            if (request.OrderIds != null && request.OrderIds.Count > 100) return "订单数不能超过100";
            EvaluateOperatorService.AutoInitEvaluate(request);
            return "ok";
        }

        [Route("api/AutoEvalOrderHis")]
        [HttpPost]
        public string AutoEvalOrderHis([FromBody]AutoSubmitEvaluateRequest request)
        {
            if (request == null) request = new AutoSubmitEvaluateRequest();
            if (request.OrderIds != null && request.OrderIds.Count > 100) return "订单数不能超过100";
            EvaluateOperatorService.AutoEvalOrderHis(request);
            return "ok";
        }
    }
}
